<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/CustomCommand.php';	
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/DaoFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/factory/CommandFactory.php';	
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/NaturalCustomer.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/LegalClient.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Place.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/ClientAddress.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Phone.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Order.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/OrderDetail.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	/*
	* Command to create a bill
	*/
	class CommandCreateOrder extends CustomCommand {
		/*
		* Constructor of the class
		*/
		public function __construct($ciRifList, $ciRif, $clientName, $secondName, 
			$firstLastName, $secondLastName, $emailAddress, $startDate, $endDate, $country, 
			$stateProvince, $city, $zone, $homePhone, $mobilePhone, $workPhone, $faxPhone, 
			$orderNumber, $orderControlNumber, $orderDate, $tax,$readStatus, $products, 
			$orderType) {
			/**
			 * Creating a client
			 */
			if ($ciRifList == 'ci') {
				$this -> clientType = 'NATURAL_CUSTOMER';
				$this -> naturalCustomer = new NaturalCustomer($ciRif, $clientName, $secondName, 
					$firstLastName, $secondLastName, $emailAddress);
			}
			
			else {
				$this -> clientType = 'LEGAL_CLIENT';
				$this -> legalClient = new LegalClient($ciRif, $clientName, $emailAddress);
			}
			
			/**
			 * Creating a place
			 */
			$this -> country = new Place("PAIS", $country);
			$this -> stateProvince = new Place("ESTADO/PROVINCIA", $stateProvince);
			$this -> city = new Place("CIUDAD", $city);
			$this -> zone = new Place("SECTOR", $zone);
			
			/**
			 * Creating an address
			 */
			$this -> clientAddress = new ClientAddress($startDate, $endDate, $ciRif, $ciRifList);
			
			/**
			 * Creating a home phone
			 */
			$homePhone = explode('.', $homePhone); 
			$homePhoneSerial = $homePhone[0];
			$homePhoneNumber = $homePhone[1];
			$this -> homePhone = new Phone('HABITACION', $homePhoneSerial, $homePhoneNumber, 
				$ciRif, $this -> clientType);
			$GLOBALS['debug'] -> log($this -> homePhone -> getTypeOwner(), "type owner");
			
			/**
			 * Creating a mobile phone
			 */
			$mobilePhone = explode('.', $mobilePhone); 
			$mobilePhoneSerial = $mobilePhone[0];
			$mobilePhoneNumber = $mobilePhone[1];
			$this -> mobilePhone = new Phone('MOVIL', $mobilePhoneSerial, $mobilePhoneNumber, 
				$ciRif, $this -> clientType);
				
			/**
			 * Creating a work phone
			 */
			$workPhone = explode('.', $workPhone); 
			$workPhoneSerial = $workPhone[0];
			$workPhoneNumber = $workPhone[1];
			$this -> workPhone = new Phone('TRABAJO', $workPhoneSerial, $workPhoneNumber, 
				$ciRif, $this -> clientType);
			
			/**
			 * Creating a fax phone
			 */
			$faxPhone = explode('.', $faxPhone); 
			$faxPhoneSerial = $faxPhone[0];
			$faxPhoneNumber = $faxPhone[1];
			$this -> faxPhone = new Phone('FAX', $faxPhoneSerial, $faxPhoneNumber, 
				$ciRif, $this -> clientType);
			
			/**
			 * Creating an order
			 */
			$this -> order = new Order($orderNumber, $orderControlNumber, $orderDate, $tax,$readStatus, $ciRif, $ciRifList,$orderType);
				
			/**
			 * Creating products
			 */
			$this -> products = $products;
		}
		
		/*
		* Method to execute to create a Purchase Order
		*/
		public function execute() {
			if (strcmp($this -> clientType, 'NATURAL_CUSTOMER') == 0) {			
				/**
				 * Creating a natural customer
				 */
				$command = CommandFactory :: createCommandCreateNaturalCustomer
					($this -> naturalCustomer -> getPkIdNumber(), 
					$this -> naturalCustomer -> getFirstName(), 
					$this -> naturalCustomer -> getSecondName(), 
					$this -> naturalCustomer -> getFirstLastName(), 
					$this -> naturalCustomer -> getSecondLastName(), 
					$this -> naturalCustomer -> getEmailAddress());
				$command -> execute();
			}
			
			else {
				/**
				 * Creating a legal client
				 */
				$command = CommandFactory :: createCommandCreateLegalClient
					($this -> legalClient -> getPkRif(), $this -> legalClient -> getName(), 
					$this -> legalClient -> getEmailAddress());
				$command -> execute();
			}
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a country
			 */
			$command = CommandFactory :: createCommandCreatePlace($this -> country);
			$command -> execute();
			$commandResult = $command -> getResult();
			
			/**
			 * If the command failed to insert a new country
			 */
			if ($commandResult == false) {
				$this -> result = $commandResult;
				return;
			}
			
			/**
			 * If the command found an existing country name
			 */
			else if($commandResult != true) {
				$this -> stateProvince -> setFkIdPlace($commandResult);
			}
			
			/**
			 * Creating a state/province
			 */
			$command = CommandFactory :: createCommandCreatePlace($this -> stateProvince);
			$command -> execute();
			$commandResult = $command -> getResult();
			
			/**
			 * If the command failed to insert a new state/province
			 */
			if ($commandResult == false) {
				$this -> result = $commandResult;
				return;
			}
			
			/**
			 * If the command found an existing state/province name
			 */
			else if($commandResult != true) {
				$this -> city -> setFkIdPlace($commandResult);
			}
			
			/**
			 * Creating a city
			 */	
			$command = CommandFactory :: createCommandCreatePlace($this -> city);
			$command -> execute();
			$commandResult = $command -> getResult();
			
			/**
			 * If the command failed to insert a new city
			 */
			if ($commandResult == false) {
				$this -> result = $commandResult;
				return;
			}
			
			/**
			 * If the command found an existing city name
			 */
			else if($commandResult != true) {
				$this -> zone -> setFkIdPlace($commandResult);
			}
			
			/**
			 * Creating a zone
			 */
			$command = CommandFactory :: createCommandCreatePlace($this -> zone);
			$command -> execute();
			$commandResult = $command -> getResult();
			
			/**
			 * If the command failed to insert a new zone
			 */
			if ($commandResult == false) {
				$this -> result = $commandResult;
				return;
			}
			
			/**
			 * If the command found an existing zone name
			 */
			else if($commandResult != true) {
				$this -> clientAddress -> setIdPlace($commandResult);
			}
			
			/**
			 * Creating a client address
			 */
			$command = CommandFactory :: createCommandCreateClientAddress
				($this -> clientAddress);
			$command -> execute();
			
			/**
			 * Creating a home phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> homePhone);
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a mobile phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> mobilePhone);
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a work phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> workPhone);
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a fax phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> faxPhone);
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating an order
			 */
			$daoFactory = DaoFactory :: getMySqlDaoFactory();
			$ibad = $daoFactory -> getMySqlOrderAccessDao();
			$orderCreation = $ibad -> createOrder($this -> order);
			
			/**
			 * Creating order details
			 */
			$GLOBALS['debug'] -> log(count($this -> products['pk_code']), "Count pk code");
			for ($i = 0; $i < count($this -> products['pk_code']); $i++) {
				$orderDetail = new OrderDetail($this -> products['pk_line'][$i], 
					$this -> products['units'][$i], $this -> products['quantity'][$i], 
					$this -> products['unit_cost'][$i], 
					$this -> products['purchase_order_number'][$i], 
					$this -> products['purchase_order_control_number'][$i], 
					$this -> products['pk_code'][$i], $this -> products['branch_rif'][$i]);
					
				$command = CommandFactory :: createCommandCreateOrderDetail($orderDetail);
				$command -> execute();
			}
			
			$GLOBALS['debug'] -> log($orderCreation, "Order creation");
	
			$this -> result = $orderCreation;
		}
	}
?>